#!/bin/bash
set -e -f -o pipefail
source "/usr/local/etc/babun.instance"
source "$babun_tools/script.sh"
source "$babun_tools/procps.sh"
source "$babun_tools/check.sh"

check_only_one_running "babun"

function usage()
{
  echo babun: Manages the instance of babun.
  echo ""
  echo "Usage:"
  echo "  \"babun update\" to fetch the newest version of babun"  
  echo "  \"babun check\" to check if the installation is correct"  
  echo "  \"babun shell\" to display the name of the user's default shell (may not be the currently used shell)"  
  echo "  \"babun shell <new_shell>\" to set the user's default shell to new_shell"   
  echo "  \"babun install\" to (re)install babun to the current user's home folder"  
  echo "  \"babun login\" to login to the user's default shell"  
  echo "  \"babun <plugin_name> cmd\" to execute cmd provided by the given plugin"
  echo "Options:"
  echo "  --help"
  echo "  --version"
  echo "  --welcome"
}

function version()
{
  echo "babun version $(cat $babun/installed/babun 2> /dev/null || echo '1.0.1 ???')"
  echo "Created and maintained by Tom Bujok (@tombujok) and Lukasz Pielak (@lukaszpielak)."
  echo "Copyright (c) 2014-2015."
}

function welcome()
{
  echo -e "Welcome to babun - the Windows shell you will love!"
  echo -e "You have got a lot of things under the hood here!"
  echo -e ""
  echo -e "   pact -> a package manager that handles installation of babun packages"
  echo -e "      pact install tar -> will install tar package on your system"
  echo -e "      pact remove tar -> will remove tar package from your system"
  echo -e ""
  echo -e "   babun -> a script that manages babun and enables auto-update"
  echo -e "      babun help -> will display babun script usage"
  echo -e "      babun check -> will check the configuration (proxy, connection, etc)"
  echo -e "      babun update -> will update babun to the newest version"
  echo -e "      babun --welcome -> will display this message again"
  echo -e ""
  echo -e "There are a couple of plugins installed by default: oh-my-zsh, pact, cacert, etc."
  echo -e "There are also a lot of dev tools such as git, svn, unix utils, etc"
  echo -e "If you would like to contribute, code up a plugin or report an issue just"
  echo -e "go to GitHub: https://github.com/babun/babun"
  echo -e ""
  echo -e "If you like the project, star it on GitHub and follow us on twitter!"
  echo -e "This project is created and maintained by Tom Bujok (@tombujok) and Lukasz Pielak (@lukaszpielak)."
}

#babun
core="/usr/local/etc/babun/source/babun-core"

# process options
noscripts=0
update=0
file=""
command=""
args=""

while test $# -gt 0
do
  case "$1" in

    --help)
      usage
      exit 0
    ;;

    --version)
      version
      exit 0
    ;;

    --welcome)
      welcome
      exit 0
    ;;

    --login)
      login
      exit 0
    ;;

    update|check|shell|install|login)
      if test "-$command-" = "--"
      then
        command=$1     
      fi
      shift

    ;;

    *)
      if [[ $args == "" ]]; then
        args="$1";
      else
        args="$args $1";
      fi
      shift
    ;;  

  esac
done


function update() 
{
  if [ "$args" != "" ] && [ "$args" != "--force" ]; then echo "ERROR: Wrong argument $args"; exit 1; fi
  echo "Executing babun update"

  permcheck=$( chmod 777 /usr/local/bin/babun 2> /dev/null || echo "FAILED" )
  if [[  $permcheck == "FAILED" ]]; then
    echo -e "ERROR: You don't have write permission to / filesystem. Aborting!"
    echo -e "Have you installed babun as admin and run it from a non-admin account? Retry as admin." 
    exit 1       
  fi  

  cp "$core"/tools/update_source.sh /tmp/update_source.sh
  /tmp/update_source.sh "$args"
  exit 0
}

function install() 
{
  if ! [[ $args == "" ]]; then echo "ERROR: Wrong argument $args"; exit 1; fi
  echo "Executing babun install"
  "$core"/plugins/install_home.sh 
  exit 0
}

function check() 
{
    if ! [[ $args == "" ]]; then echo "ERROR: Wrong argument $args"; exit 1; fi
    echo "Executing babun check"
    babun_check
}

function shell()
{
    passwd=$( sort -u /etc/passwd )
    if ! [[ -z "$passwd" ]]; then
        echo "$passwd" > /etc/passwd
    fi
    newshell=$args
    currshell=$( awk "/^$USERNAME/ { print $1 }" /etc/passwd | grep -oh "/bin/.*sh" )
    if [[ $newshell == "" ]]; then        
        echo "$currshell"
    else
        if ! [ -f $newshell ]; then
           echo "ERROR: Cannot set shell to $newshell. Specified shell does not exist!"
           exit 1
        fi
        echo "$currshell"
        echo "$newshell"        
        sed -i "/^$USERNAME/s|/bin/.*sh|$newshell|" "/etc/passwd" || "ERROR: Changing shell to $newshell failed"
    fi
}

function login()
{
    proc_shell_login
}

case "$command" in

  update)
  
    update
  ;;


  check)

    check
  ;;

  shell)

    shell
  ;;

  install)

    install
  ;;

  login)

    login
  ;;

  *)

    # forward the execution to the plugin in the babun <plugin_name> cmd format
    cmds=($args)
    cmdsLength=${#cmds[@]}
    if [ $cmdsLength -gt 0 ]; then
      if [[ -f "$babun_plugins/${cmds[0]}/exec.sh" ]]; then
        "$babun_plugins/${cmds[0]}/exec.sh" ${cmds[@]:1}
      else
        usage
      fi
    else
      usage
    fi
  ;;

esac


